﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Mailjet.Client;
using Newtonsoft.Json;

namespace CRM.HttpProxy
{
    public class MiHttpProxy
    {
        private readonly HttpClient _httpClient;

        public string Url { get; init; } = "http://httpproxy.miwaimao.com/";

        public MiHttpProxy(HttpClient httpClient)
        {
            httpClient.BaseAddress = new Uri(Url);
            _httpClient = httpClient;
        }

        /// <summary>
        /// 根据关键词、国家查询域名列表
        /// </summary>
        /// <returns></returns>
        public Task<HttpResponseMessage> SerpMaster(string keywords, string nation = "")
        {
            _httpClient.DefaultRequestHeaders.Remove("ProxyUrl");
            _httpClient.DefaultRequestHeaders.Remove("SerpmasterKey");
            _httpClient.DefaultRequestHeaders.Add("ProxyUrl", $"https://rt.serpmaster.com/?q={WebUtility.UrlEncode(keywords)}&parse=true&num=100&&geo={nation}");
            _httpClient.DefaultRequestHeaders.Add("SerpmasterKey", "True");
            return _httpClient.GetAsync(string.Empty);
        }
        
        /// <summary>
        /// 根据关键词、国家查询域名列表
        /// </summary>
        /// <returns></returns>
        public Task<HttpResponseMessage> SerpMasterV2(string keywords, string nation = "")
        {
            _httpClient.DefaultRequestHeaders.Remove("ProxyUrl");
            _httpClient.DefaultRequestHeaders.Remove("SerpmasterKey");
            _httpClient.DefaultRequestHeaders.Add("ProxyUrl", "https://scrape.smartproxy.com/v1/tasks");
            _httpClient.DefaultRequestHeaders.Add("SerpmasterKey", "True");
            return _httpClient.PostAsJsonAsync(string.Empty, new
            {
                target = "bing_search",
                query = WebUtility.UrlEncode(keywords),
                parse = true,
                // 最大页数为50
                num_pages = 50,
                geo = nation
            });
        }
        
        /// <summary>
        /// 根据关键词、国家查询域名列表
        /// </summary>
        /// <returns></returns>
        public Task<HttpResponseMessage> SerpCenter(string keywords, string nation = "")
        {
            var httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("http://serp-center.yimisoft.com/serp");
            var result = httpClient.PostAsync(string.Empty, new StringContent(JsonConvert.SerializeObject(new
            {
                query = keywords,
                nation = nation,
            }), Encoding.UTF8, "application/json"));
            return result;
        }

        public Task<HttpResponseMessage> PostAsJsonAsync(string camelToStrike, Dictionary<string, string> toDictionary)
        {
            _httpClient.DefaultRequestHeaders.Remove("ProxyUrl");
            _httpClient.DefaultRequestHeaders.Add("ProxyUrl", camelToStrike);
            return _httpClient.PostAsJsonAsync(string.Empty, toDictionary);
        }
    }
}